package com.example.demo.YsyTest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.HTTPTest.util.OKHttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class YsyApi {

    /**
     * 返回成功码
     */
    private static final String RETURN_CODE = "200";

    /**
     * 萤石云API公共前缀
     */
    private static final String BASE_URL = "https://open.ys7.com/api/lapp";

    public static void main(String[] args) {
        String token = "at.479ew5s5aj4a9fy90h635zaq7j821ilg-8dwf0et1rg-0f3gd0p-neiv9xjmz";
        String deviceSerial = "K99501823";
        Integer channelNo = 1;
        String index = "2";

        // 获取单个设备信息
//        JSONObject jsonObject = deviceInfo(token, deviceSerial);

        // 获取设备抓拍图片
//        JSONObject jsonObject = deviceCapture(token, deviceSerial, channelNo);

        // 添加预制点
//        JSONObject jsonObject = addPresetPoint(token, deviceSerial, channelNo);

        // 删除预制点
//        JSONObject jsonObject = deletePresetPoint(token, deviceSerial, channelNo, index);

        // 调用预置点
        JSONObject jsonObject = invokePresetPoint(token, deviceSerial, channelNo, index);

        System.out.println(jsonObject);
    }

    /**
     * 获取单个设备信息
     *
     * @param token        Token
     * @param deviceSerial 设备序列号
     * @return
     */
    public static JSONObject deviceInfo(Object token, String deviceSerial) {

        String url = BASE_URL + "/device/info?accessToken=" + token + "&deviceSerial=" + deviceSerial;
        Map<String, Object> httpResult = new HashMap<>(0);
        try {
            httpResult = OKHttpUtil.dopost(url, new JSONObject());
        } catch (IOException e) {
            log.error("获取单个设备信息失败，accessToken：{},deviceSerial：{}", token, deviceSerial);
        }
        if (ObjectUtils.isNotEmpty(httpResult)) {
            JSONObject jsonObject = JSON.parseObject(httpResult.get("result").toString());
            if (RETURN_CODE.equals(jsonObject.get("code").toString()) && ObjectUtils.isNotEmpty(jsonObject.get("data"))) {
                return jsonObject;
//                返回: {"msg":"操作成功","code":"200","data":{"alarmSoundMode":2,"riskLevel":0,"defence":0,"netType":"wire","updateTime":1677666169000,"offlineNotify":0,"deviceName":"省沿海项目-3","deviceSerial":"K99501823","localName":"球机3","netAddress":"125.124.150.184","parentCategory":"COMMON","model":"DS-2DE4423IW-D/GLT/XM","category":"UNKNOWN","signal":"100%","status":1,"isEncrypt":0}}
            }
        }
        log.info("获取萤石云平台token失败!");
        return null;
    }

    /**
     * 获取设备抓拍图片
     *
     * @param token        Token
     * @param deviceSerial 设备序列号
     * @param channelNo    通道号
     * @return JSONObject
     */
    public static JSONObject deviceCapture(Object token, String deviceSerial, Integer channelNo) {

        String url = BASE_URL + "/device/capture?accessToken=" + token + "&deviceSerial=" + deviceSerial + "&channelNo=" + channelNo;
        Map<String, Object> httpResult = new HashMap<>(0);
        try {
            httpResult = OKHttpUtil.dopost(url, new JSONObject());
        } catch (IOException e) {
            log.error("设备抓拍图片失败，accessToken：{},deviceSerial：{},channelNo:{}", token, deviceSerial, channelNo);
        }
        if (ObjectUtils.isNotEmpty(httpResult)) {
            JSONObject jsonObject = JSON.parseObject(httpResult.get("result").toString());
            if (RETURN_CODE.equals(jsonObject.get("code").toString()) && ObjectUtils.isNotEmpty(jsonObject.get("data"))) {
                return jsonObject;
//                返回: {"msg":"操作成功","code":"200","data":{"picUrl":"https://fastdfs-gateway.ys7.com/065c/1/capture/003kLGf3HpHCe8AkV7ZPyoycI3qPLJ1.jpg?Expires=1693900902&OSSAccessKeyId=LTAIzI38nEHqg64n&Signature=YU5fSHFcYrZGe1%2BU8cqj508XZts%3D"}}
            }
        }
        log.info("获取萤石云平台token失败!");
        return null;
    }

    /**
     * 添加预制点
     *
     * @param token        Token
     * @param deviceSerial 设备序列号
     * @param channelNo    通道号
     * @return JSONObject
     */
    public static JSONObject addPresetPoint(Object token, String deviceSerial, Integer channelNo) {

        String url = BASE_URL + "/device/preset/add?accessToken=" + token + "&deviceSerial=" + deviceSerial + "&channelNo=" + channelNo;
        Map<String, Object> httpResult = new HashMap<>(0);
        try {
            httpResult = OKHttpUtil.dopost(url, new JSONObject());
        } catch (IOException e) {
            log.error("添加预制点失败，accessToken：{},deviceSerial：{},channelNo:{}", token, deviceSerial, channelNo);
        }
        if (ObjectUtils.isNotEmpty(httpResult)) {
            JSONObject jsonObject = JSON.parseObject(httpResult.get("result").toString());
            if (RETURN_CODE.equals(jsonObject.get("code").toString()) && ObjectUtils.isNotEmpty(jsonObject.get("data"))) {
                return jsonObject;
//                返回: {"msg":"操作成功","code":"200","data":{"index":2}}
            }
        }
        log.info("获取萤石云平台token失败!");
        return null;
    }

    /**
     * 删除预制点(PS:删除预置点不存在依旧删除成功)
     *
     * @param token        Token
     * @param deviceSerial 设备序列号
     * @param channelNo    通道号
     * @param index        预置点索引
     * @return JSONObject
     */
    public static JSONObject deletePresetPoint(Object token, String deviceSerial, Integer channelNo, String index) {
        String url = BASE_URL + "/device/preset/clear?accessToken=" + token + "&deviceSerial=" + deviceSerial + "&channelNo=" + channelNo + "&index=" + index;
        Map<String, Object> httpResult = new HashMap<>(0);
        try {
            httpResult = OKHttpUtil.dopost(url, new JSONObject());
        } catch (IOException e) {
            log.error("删除预制点失败，accessToken：{},deviceSerial：{},channelNo:{}", token, deviceSerial, channelNo);
        }
        if (ObjectUtils.isNotEmpty(httpResult)) {
            JSONObject jsonObject = JSON.parseObject(httpResult.get("result").toString());
            if (RETURN_CODE.equals(jsonObject.get("code").toString())) {
                return jsonObject;
//                返回: {"msg":"操作成功","code":"200"}
            }
        }
        log.info("获取萤石云平台token失败!");
        return null;
    }

    /**
     * 调用预置点(PS:调用预置点不存在依旧调用成功)
     *
     * @param token Token
     * @param deviceSerial 设备序列号
     * @param channelNo 通道号
     * @param index 预置点索引
     * @return JSONObject
     */
    public static JSONObject invokePresetPoint(Object token, String deviceSerial, Integer channelNo, String index) {
        String url = BASE_URL + "/device/preset/move?accessToken=" + token + "&deviceSerial=" + deviceSerial + "&channelNo=" + channelNo + "&index=" + index;
        Map<String, Object> httpResult = new HashMap<>(0);
        try {
            httpResult = OKHttpUtil.dopost(url, new JSONObject());
        } catch (IOException e) {
            log.error("调用预制点失败，accessToken：{},deviceSerial：{},channelNo:{}", token, deviceSerial, channelNo);
        }
        if (ObjectUtils.isNotEmpty(httpResult)) {
            JSONObject jsonObject = JSON.parseObject(httpResult.get("result").toString());
            if (RETURN_CODE.equals(jsonObject.get("code").toString())) {
                return jsonObject;
//                返回: {"msg":"操作成功","code":"200"}
            }
        }
        log.info("获取萤石云平台token失败!");
        return null;
    }
}
